# -*- coding: utf-8 -*-
# @Time    : 2025/1/23 上午5:54
# @Author  : lh
# @Email   : lhhlhh822@peatlmail.com
# @File    : announce_list_page.py
# @Software: PyCharm
from selenium.webdriver.support.select import Select
from pages.base_page import BasePage
from selenium.webdriver.common.by import By


class AnnounceList(BasePage):
    url = "/crm/index.php?m=announcement"
    # 定位器
    field_locator = (By.ID, 'field')
    condition_locator = (By.ID, 'condition')
    search_locator = (By.ID, 'search')
    search_button_locator = (By.CSS_SELECTOR, '#searchForm > li:nth-child(4) > button')
    stop_locator = (
        By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(1) > td:nth-child(7) > a:nth-child(1)')
    start_locator = (
        By.CSS_SELECTOR, '#form1 > table > tbody')
    search_result_locator = (By.CSS_SELECTOR, '#form1 > table > tbody')
    tips_locator = (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-success')
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')

    #选择字段下拉框
    def select_field(self, field):
        field_element = Select(self.find_element(self.field_locator))
        field_element.select_by_visible_text(field)

    #选择状态下拉框的内容
    def select_condition(self, condition):

        condition_element = Select(self.find_element(self.condition_locator))
        condition_element.select_by_visible_text(condition)

    #输入搜索内容
    def input_search_keys(self, keys):

        self.find_element(self.search_locator).send_keys(keys)

    def click_search(self):
        self.find_element(self.search_button_locator).click()

    #发布或停用公告
    def stop_announcement(self, is_start=True):
        state = self.find_element(self.stop_locator)
        state_text = state.text
        # 如果需要发布
        if is_start:
            # 如果当前状态为显示为发布，就是停用状态，则点击
            if state_text == "发布":
                state.click()
        else:
            # 如果当前状态显示为停用，就是发布状态，则点击
            if state_text == "停用":
                state.click()

    #发布或停用公告
    def start_announcement(self, is_start=False):

        state = self.find_element(self.start_locator).find_elements(By.CSS_SELECTOR, 'tr')[-1]
        state = state.find_elements(By.CSS_SELECTOR, 'td')[-1].find_element(By.CSS_SELECTOR, ':nth-child(1)')
        state_text = state.text
        # 如果需要发布
        if is_start:
            # 如果当前状态为显示为发布，就是停用状态，则点击
            if state_text == "发布":
                state.click()
        else:
            # 如果当前状态显示为停用，就是发布状态，则点击
            if state_text == "停用":
                state.click()

    #获取搜索结果文本
    def get_search_text(self):

        return self.find_element(self.search_result_locator).text

    #停用或发布公告的提示信息
    def get_tips(self):
        return self.find_element(self.tips_locator).text

        # 返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text